module(...,package.seeall)

require"luasql.sqlite3"

--local db_path = "D:/test-area/getItem/gwsj/google svn/duoluo1028/item.datafile"
local db_path = "item.datafile"

DbHandler = {
	db_path,
}

function DbHandler:new(path)
 	-- body
 	local o = {}
 	setmetatable(o,self)
 	self.__index = self
	self.db_path = path or db_path
 	self.env = assert(luasql.sqlite3())
 	self.handler = assert(self.env:connect(self.db_path))
 	return o
 end

 function DbHandler:query(sql)
 	-- body
 	local res = assert(self.handler:execute(sql))
	return res
 end

 function DbHandler:fetch(res,t,mode)
 	-- body
 	--return a table
 	if not res then
 		error('res got nil,in fetch method')
 	end
 	if type(t) ~= 'table' then
 		t = {}
 	end
 	mode = mode or 'a'
 	if mode ~= 'a' and mode ~= 'n' then
 		mode = 'a'
 	end
 	--print('out:',res,t,mode)
	return function(res)
		--print(res,t,mode)
		if(nil~= res:fetch(t, mode)) then
			return t
		else
			return nil
		end
	end,res
 end

return _M
